﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;

namespace BookClubIt
{
    public partial class editbookclub : BasePage
    {
        public int CurrentUserId { get; set; }
        public int bid { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DTO.BookClub club = BookClubProcessorInstance.GetBookClubById(CID);

                var club_Name = club.Name.ToString();
                lblClubName.Text = club.Name.ToString();
                Page.Title = "Edit " + club_Name;
                Page.MetaDescription = club.Description.ToString();

                //Set input values

                string avatarpath = "../avatars/club/";
                imgClubAvatar.ImageUrl = avatarpath + club.AvatarURL.ToString();
                imgClubAvatar.AlternateText = club.Name.ToString();
                txtClubName.Text = club.Name.ToString();
                txtDescription.InnerText = club.Description.ToString();
                txtRules.InnerText = club.Rules.ToString();
                txtWebsite.Text = club.Website.ToString();
                hdnCreatedDate.Value = club.CreatedDate.ToString();
                // set se;ected club type
                rblClubType.SelectedIndex = club.ClubType - 1;


                DTO.GenreTypes get_genres = BookClubProcessorInstance.GetGenreTypes();

                ddlGenres.DataSource = get_genres;
                ddlGenres.DataTextField = "GenreName";
                ddlGenres.DataValueField = "Id";
                ddlGenres.DataBind();
                ddlGenres.SelectedIndex = club.GenreType - 1;

                if (Convert.ToInt32(club.GroupMeetsRealLife) == 1)
                    chkRealMeet.Attributes.Add("checked", "checked");

                if (Convert.ToInt32(club.NonLeaderAddEvents) == 1)
                    chkNonModAddEvents.Attributes.Add("checked", "checked");

                if (Convert.ToInt32(club.AdultGroup) == 1)
                    chk18andOver.Attributes.Add("checked", "checked");


                if (Convert.ToInt32(club.NonLeaderAddPolls) == 1)
                    chkNonModAddPolls.Attributes.Add("checked", "checked");

                if (Convert.ToInt32(club.NonLeaderAddTopics) == 1)
                    chkNonModAddTopics.Attributes.Add("checked", "checked");


            }
        }
         protected void btnEditClub_Click(object sender, EventArgs e)
        {
            int NonPolls = 0;
            int NonEvents = 0;
            int AdultGroup = 0;
            int NonTopics = 0;
            int RealMeet = 0;

            if (chkNonModAddEvents.Checked)
                NonEvents = 1;

            if (chk18andOver.Checked)
                AdultGroup = 1;

            if (chkNonModAddPolls.Checked)
                NonPolls = 1;


            if (chkNonModAddTopics.Checked)
                NonTopics = 1;

            if (chkRealMeet.Checked)
                RealMeet = 1;

            int selectedClubType = Convert.ToInt32(rblClubType.SelectedValue);
            int selectedGenre = Convert.ToInt32(ddlGenres.SelectedItem.Value);

            DTO.BookClub bookclub = new DTO.BookClub
            {
                Id = CID,
                Name = txtClubName.Text.ToString(),
                ClubType = selectedClubType,
                GenreType = selectedGenre,
                CreatedDate = Convert.ToDateTime(hdnCreatedDate.Value),
                Description = txtDescription.InnerText.ToString(),
                Rules = txtRules.InnerText.ToString(),
                Website = txtWebsite.Text.ToString(),
                AdultGroup = AdultGroup,
                NonLeaderAddEvents = NonEvents,
                NonLeaderAddPolls = NonPolls,
                NonLeaderAddTopics = NonTopics,
                GroupMeetsRealLife = RealMeet,
                AvatarURL = imgClubAvatar.ImageUrl.ToString().Split('/').Last()
            };

            BookClubProcessorInstance.SaveBookClub(bookclub);
         }


         protected void btnUpload_Click(object sender, EventArgs e)
         {
             string username = HttpContext.Current.User.Identity.Name.ToString();
             DTO.Member member = MemberProcessorInstance.GetMemberByUsername(username);
             int CurrentUserId = member.Id;

             int bid = 0;
             int.TryParse(Request.QueryString["id"], out bid);


             string filename = UploadImage.FileName;
             if (UploadImage.PostedFile.ContentLength != 0)
             {
                 UploadImage.SaveAs(Server.MapPath("~/avatars/club/" + filename));
                 BookClubProcessorInstance.UpdateBookClubAvatar(bid, filename);
                 Page.Response.Redirect(Page.Request.Url.ToString(), true);
             }
         }

    }
}